import Vue from 'vue'
import Vuex from 'vuex'
import { getProvider, showToast, uniLogin } from "../utils/promise.js"
import { getUserMsg, userLogin } from '../api/user.js';
import { mallConfig } from '../api/shop.js';
import Big from "big.js";

Vue.use(Vuex)

export const initUserInfo = {
  appleBind: false,
  avatar: null,
  birthday: null,
  createTime: "",
  id: 0,
  nickname: "",
  sex: null,
  telephone: null,
  telephoneBind: false,
  wechatBind: false,
}

export default new Vuex.Store({
	state: {
		agreePrivacy: uni.getStorageSync('agreePrivacy') || false, // 同意隐私协议
		getUserInfo: false,
		getUserToken: false,
		getMallConfig: false,
		userToken: uni.getStorageSync('userToken') || null,
		userInfo: uni.getStorageSync('userInfo') || JSON.parse(JSON.stringify(initUserInfo)),
		privateKey: '',
		mallConfig: {},
		loginErrorTime: 0,
		timeOutProcess: false,
		//token 是否可用
		tokenInvalid: false,
		//flutter是否加载完成
		flutterDone: false,
	},
	mutations: {
		setAgreePrivacy(state, data) {
			uni.setStorageSync('agreePrivacy', data);
			state.agreePrivacy = data;
		},
		setUserToken(state, data) {
			state.userToken = data;
			uni.setStorageSync('userToken', data);
			if (data) {
				this.dispatch('getUserInfo');
				this.dispatch("getMallConfig")
			}
		},
		setUserInfo(state, data) {
			state.userInfo = data;
			uni.setStorageSync('userInfo', data)
		},
		setGetUserToken(state, data) {
			state.getUserToken = data;
		},
		setGetUserInfo(state, data) {
			state.getUserInfo = data;
		},
		setFlutterDone(state, data) {
			state.flutterDone = data;
		},
		setGetMallConfig(state, data) {
			state.getMallConfig = data;
		},
		setMallConfig(state, data) {
			state.mallConfig = data;
		},
		setPrivateKey(state, data) {
			state.privateKey = data;
		},
		setLoginErrorTime(state, data) {
			state.loginErrorTime = state.loginErrorTime + 1;
		},
		setTimeOutProcess(state, data) {
			state.timeOutProcess = data;
		},
		setTokenInvalid(state, data) {
			state.tokenInvalid = data;
		},
	},
	actions: {
		login({
			commit,
			state
		}) {
			getProvider({
				service: "oauth"
			}).then((res) => {
				// console.log(res)
				let provider = res.provider[0]
				uniLogin({
					provider: provider
				}).then((res) => {
					// console.log(res)
					let code = res.code
					// console.log(code)
					userLogin(code).then((res) => {
						// console.log(res)
						const {data} = res
						// console.log(data.token)
						commit('setTokenInvalid', false);
						commit('setTimeOutProcess', false);
						commit("setUserToken", data.token);
					}).catch((err) => {
						console.log(err)
						return err
					})
				}).catch((err) => {
					console.log(err)
					return err
				})
			})
		},
		getUserInfo({
			commit,
			state
		}) {
			getUserMsg().then((res) => {
				// console.log(res)
				const {data} = res
				commit("setUserInfo", data)
			}).catch((err) => {
				return err
			})
		},
		getMallConfig({
			commit,
			state
		}) {
			mallConfig().then((res) => {
				// console.log(res)
				const {code, data, msg} = res
				if(code !== 200) {
					showToast({
						title: "请求失败"
					})
					return
				}
					data.expressLimit = new Big(res.data.expressLimit).div(100);
					data.expressPrice = new Big(res.data.expressPrice).div(100);
					commit("setMallConfig", data)
			}).catch((err) => {
				console.log(err)
			})
		},
	}
})
